/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.task.controller  
 */
package com.apache.task.controller;

import com.apache.api.vo.ResultMsg;
import com.apache.database.model.TaskEntity;
import com.apache.task.api.ScheduledExecutor;
import com.apache.task.util.XmlReader;
import com.apache.tools.StrUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  定时任务管理Action
 * @author Hou Dayu 创建时间：2016-1-21
 * @Copyright(c)2014:
 */
@Controller
@RequestMapping("/tasks/")
public class TimeTaskAction {

	@RequestMapping("list.action")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("timetask/task-list");
	}

	@RequestMapping("list!data.action")
	@ResponseBody
	public Object data(HttpServletRequest request, String page, String rows) {
		List<TaskEntity> list = ScheduledExecutor.newInstance().getTasks();
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", list.size());
		mapData.put("rows", list);
		return mapData;
	}

	//开启线程
	@RequestMapping("start.action")
	@ResponseBody
	public ResultMsg start(HttpServletRequest request, String key) {
		if (StrUtil.isNull(key)) {//开启所有线程
			ScheduledExecutor.newInstance().start();
		} else {//开户指定线程
			ScheduledExecutor.newInstance().start(key);
		}
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		return rmsg;
	}

	/**
	 * description:  添加线程跳转页面
	 */
	@RequestMapping("add.action")
	public ModelAndView add(HttpServletRequest request, String key) {
		TaskEntity task = new TaskEntity();
		if (StrUtil.isNotNull(key)) {
			List<TaskEntity> list = ScheduledExecutor.newInstance().getTasks();
			for (TaskEntity te : list) {
				if (key.equals(te.getTaskKey())) {
					task = te;
					break;
				}
			}
		}
		ModelAndView view = new ModelAndView("timetask/task-save");
		view.addObject("item", task);
		return view;
	}

	/**
	 * description:  添加线程
	 */
	@RequestMapping(value = "/save.action", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, TaskEntity item) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		String isAdd = "false";
		if (StrUtil.isNull(item.getTaskKey())) {
			isAdd = "true";
		}
		String id = "";
		boolean mark = false;
		if (item.getIntervalType() == 1) {//如果选择按每天定时执行,小时项未填写则默认此项为1
			if (item.getHour() <= 1) {
				item.setHour(1l);
			}
		}
		if ("false".equals(isAdd) && item != null) {
			List<TaskEntity> list = ScheduledExecutor.newInstance().getTasks();
			int i = 0;
			for (TaskEntity te : list) {
				if (item.getTaskKey().equals(te.getTaskKey())) {
					if (te.isOpenStart()) {
						rmsg.setFlag("F");
						rmsg.setMsg("任务已开启不能修改");
						return rmsg;
					}
					list.remove(te);
					break;
				}
				i++;
			}
			//ScheduledExecutor.newInstance().setTask(item);
			list.add(i, item);
			mark = XmlReader.newInstance().writeXml(list);
		} else {
			List<TaskEntity> list = ScheduledExecutor.newInstance().getTasks();
			id = XmlReader.newInstance().noZero(list.size() + "", 3, "1");
			item.setTaskKey(id);
			list.add(item);
			mark = XmlReader.newInstance().writeXml(list);
			if (item.getTaskStatus() == 1) {
				ScheduledExecutor.newInstance().start(id);
			}
		}
		if (("false".equals(isAdd) && !mark) || ("true".equals(isAdd) && StrUtil.isNull(id))) {
			rmsg.setFlag("F");
			rmsg.setMsg("操作失败！");
		}
		return rmsg;
	}

	@RequestMapping("del.action")
	@ResponseBody
	public ResultMsg delTask(HttpServletRequest request, String key) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		if (StrUtil.isNotNull(key)) {//开启所有线程
			List<TaskEntity> list = ScheduledExecutor.newInstance().getTasks();
			for (TaskEntity te : list) {
				if (key.equals(te.getTaskKey())) {
					if (te.isOpenStart()) {
						rmsg.setFlag("F");
						rmsg.setMsg("任务已开启不能删除");
						return rmsg;
					}
					list.remove(te);
					break;
				}
			}
			XmlReader.newInstance().writeXml(list);
		}
		return rmsg;
	}

	//停止线程
	@RequestMapping("stop.action")
	@ResponseBody
	public ResultMsg stop(HttpServletRequest request, String key) {

		if (StrUtil.isNull(key)) {//开启所有线程
			ScheduledExecutor.newInstance().stopTasks();
		} else {//开户指定线程
			ScheduledExecutor.newInstance().stopTaskByKey(key);
		}
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		return rmsg;
	}
}
